package rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产者
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("110.41.84.205");
        factory.setPort(5672); // 端口号
        factory.setUsername("study"); // 账号
        factory.setPassword("study"); // 密码
        factory.setVirtualHost("Demo"); // 虚拟主机

        Connection connection = factory.newConnection();// 建立连接
        // 建立通道
        Channel channel = connection.createChannel();
        // 声明队列
        /**
         * queueDeclare(String var1, boolean var2, boolean var3, boolean var4, Map<String, Object> var5)
         * var2 (durable) : 是否持久化.true-设置队列为持久化, 待久化的队列会存盘,服务器重启之后, 消息不丢失
         * var3 (exclusive) 是否独占, 只能有⼀个消费者监听队列, 当Connection关闭时, 是否删除队列
         * var4 (autoDelete) 是否⾃动删除, 当没有Consumer时, ⾃动删除掉
         * var5 (arguments) ⼀些参数
         */
        channel.queueDeclare("hello",true,false,false,null);
        // 发送消息

        String msg = "hello RabbitMq~";
        /**
         * basicPublish(String var1, String var2, AMQP.BasicProperties var3, byte[] var4)
         * basicPublish(虚拟主机, 队列, 配置, 信息)
         */
        channel.basicPublish("","hello",null,msg.getBytes());
        System.out.println("发送成功~");
        // 释放资源
        channel.close();
        connection.close();
    }
}
